package com.earl.eatbao.dao;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.earl.eatbao.commons.dao.BaseRepsitoty;
import com.earl.eatbao.domain.sercurity.Role;
import com.earl.eatbao.domain.sercurity.User;


public interface UserDao extends BaseRepsitoty<User,Long>{

//	User findOneByUserName(String username);

	@Query("select r from User u, UserRole ur,Role r where u.id=ur.userId and ur.roleId=r.id and u.id =:id")
	List<Role> findRole(@Param("id") Long userId);

	/**
	 * 找到用户的权限
	 * @author 黄祥谦.
	 * @param id
	 * @return
	 */
	@Query("select p.privilegeCode from User u,UserRole ur,Role r,RolePrivilege rp ,Privilege p where u.id=ur.userId and ur.roleId=r.id and r.id=rp.roleId and rp.privilegeId = p.id and u.id =:userId")
	List<String> findPrivilegeCode(@Param("userId")Long id);

	/**
	 * 找到用户的角色名称.
	 * @author 黄祥谦.
	 * @param id
	 * @return
	 */
	@Query("select r.roleName from User u, UserRole ur,Role r where u.id=ur.userId and ur.roleId=r.id and u.id =:id")
	List<String> findRoleName(@Param("id") Long id);

	User findUniqueByAccount(String username);

}